package com.xueyi.aixihu.service.impl;

import com.xueyi.aixihu.entity.vo.*;
import com.xueyi.aixihu.exception.SzException;
import com.xueyi.aixihu.mapper.LocalDtNewsSortMapper;
import com.xueyi.aixihu.reqeust.FindClassifyListByPidReq;
import com.xueyi.aixihu.reqeust.FindClassifyReq;
import com.xueyi.aixihu.response.DtNewsSortListResponse;
import com.xueyi.aixihu.response.FindThreeClassifyByIdResponse;
import com.xueyi.aixihu.response.TwoClassifyResponse;
import com.xueyi.aixihu.response.base.BaseResponse;
import com.xueyi.aixihu.service.DtNewsSortService;
import com.xueyi.aixihu.status.SystemCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @authoer:hxs
 * @createDate:2022/8/16
 * @description: 文章、社区分类操作业务
 */
@Service
@Slf4j
public class DtNewsSortServiceImpl implements DtNewsSortService {
    @Autowired
    private LocalDtNewsSortMapper dtNewsSortMapper;

    /**
     * 页面初始化时查询分类
     *
     * @return
     */
    @Override
    public DtNewsSortListResponse findAllClassify() {
        List<SelectVo> oneMenu = new ArrayList<>();
        List<SelectVo> twoMenu = new ArrayList<>();
        List<SelectVo> threeMenu = new ArrayList<>();
        List<SelectVo> fourMenu = new ArrayList<>();


        //查询所有的一级菜单
        oneMenu = dtNewsSortMapper.findChildMenuByPid(0);
        if (!(oneMenu.size() < 1)) {
            //默认查询第一个一级菜单的子菜单
            twoMenu = dtNewsSortMapper.findChildMenuByPid(oneMenu.get(0).getId());

            if (!(twoMenu.size() < 1)) {
                threeMenu = dtNewsSortMapper.findChildMenuByPid(twoMenu.get(0).getId());

                if (!(threeMenu.size() < 1)) {
                    fourMenu = dtNewsSortMapper.findChildMenuByPid((threeMenu.get(0).getId()));
                }

            }
        }

        //封装返回数据
        DtNewsSortListResponse dtNewsSortListResponse =
                new DtNewsSortListResponse().oneMenus(oneMenu).twoMenus(twoMenu).threeMenus(threeMenu).fourMenu(fourMenu);


        return dtNewsSortListResponse;
    }


    /**
     * 通过一级菜单id查询二级菜单以及三级菜单
     *
     * @param findClassifyReq
     * @return
     */
    @Override
    public TwoClassifyResponse findTwoClassifyById(FindClassifyReq findClassifyReq) throws SzException {
        findClassifyReq.format();

        List<SelectVo> twoMenu = new ArrayList<>();
        List<SelectVo> threeMenu = new ArrayList<>();
        List<SelectVo> fourMenu = new ArrayList<>();

        //通过一级分类id查询二级分类列表
        twoMenu = dtNewsSortMapper.findChildMenuByPid(findClassifyReq.getPid());


        if (!(twoMenu.size() < 1)) {
            //默认查询第一个二级分类的子分类
            threeMenu = dtNewsSortMapper.findChildMenuByPid(twoMenu.get(0).getId());

            if (!(threeMenu.size() < 1)) {
                fourMenu = dtNewsSortMapper.findChildMenuByPid((threeMenu.get(0).getId()));
            }
        }

        TwoClassifyResponse twoClassifyResponse = new TwoClassifyResponse().twoMenus(twoMenu).threeMenus(threeMenu).fourMenus(fourMenu);


        return twoClassifyResponse;
    }

    /**
     * 通过二级查三级
     */
    @Override
    public FindThreeClassifyByIdResponse findFourClassifyById(FindClassifyListByPidReq findClassifyReq) {
        findClassifyReq.format();

        List<SelectVo> threeMenu = new ArrayList<>();
        List<SelectVo> fourMenu = new ArrayList<>();
        //通过二级分类id查询三级分类
        threeMenu = dtNewsSortMapper.findChildMenuByPids(findClassifyReq.getPidStr());

        if (!(threeMenu.size() < 1)) {
            fourMenu = dtNewsSortMapper.findChildMenuByPid((threeMenu.get(0).getId()));
        }
        FindThreeClassifyByIdResponse response = new FindThreeClassifyByIdResponse();
        response.setThreeMenus(threeMenu);
        response.setFourMenus(fourMenu);

        return response;
    }

    /**
     * 通过二级分类id查询三级分类
     *
     * @param findClassifyReq
     * @return
     */
    @Override
    public List<SelectVo> findThreeClassifyById(FindClassifyListByPidReq findClassifyReq) throws SzException {
        findClassifyReq.format();

        //通过三级分类id查询四级分类
        return dtNewsSortMapper.findChildMenuByPids(findClassifyReq.getPidStr());
    }

    /**
     * 查询联级菜单数据
     *
     * @return
     */
    @Override
    public List<CascaderDataVo> findCascaderData() {
        //获取所有的分类
        List<DtNewsSortVo> cascaderData = dtNewsSortMapper.findCascaderData();
        //创建一级分类
        List<CascaderDataVo> flag = new ArrayList<>();
        for (DtNewsSortVo item : cascaderData) {
            if (0 == item.getPid()) {
                //封装数据
                CascaderDataVo obj = new CascaderDataVo();
                obj.setValue(item.getId());
                obj.setLabel(item.getName());

                //创建二级分类
                List<CascaderDataVo> twoList = new ArrayList<>();
                for (DtNewsSortVo item1 : cascaderData) {
                    if (item.getId().equals(item1.getPid())) {
                        CascaderDataVo obj1 = new CascaderDataVo();
                        obj1.setValue(item1.getId());
                        obj1.setLabel(item1.getName());
                        twoList.add(obj1);

                        //创建三级分类
                        List<CascaderDataVo> threeList = new ArrayList<>();
                        for (DtNewsSortVo item2 : cascaderData) {
                            if (item1.getId().equals(item2.getPid())) {
                                CascaderDataVo obj2 = new CascaderDataVo();
                                obj2.setValue(item2.getId());
                                obj2.setLabel(item2.getName());
                                threeList.add(obj2);


                                //创建四级分类
                                List<CascaderDataVo> fourList = new ArrayList<>();
                                for (DtNewsSortVo item3 : cascaderData) {
                                    if (item2.getId().equals(item3.getPid())) {
                                        CascaderDataVo obj3 = new CascaderDataVo();
                                        obj3.setValue(item3.getId());
                                        obj3.setLabel(item3.getName());
                                        fourList.add(obj3);
                                    }
                                }
                                obj2.setChildren(fourList);

                            }
                        }
                        obj1.setChildren(threeList);
                    }
                }
                obj.setChildren(twoList);
                flag.add(obj);
            }
        }
        return flag;
    }

    /**
     * 查询出树级结果的菜单
     *
     * @return
     */
    @Override
    public List<DtNewsSortTreeVo> findTree() {

        //开始查询父级菜单
        List<DtNewsSortTreeVo> parentTree = dtNewsSortMapper.findParentTree();

        for (DtNewsSortTreeVo dtNewsSortTreeVo : parentTree) {
            //查询二级菜单并封装进父级list中
            List<DtNewsSortTreeVo> childrenTree = dtNewsSortMapper.findChildrenTree(dtNewsSortTreeVo.getId());
            dtNewsSortTreeVo.setChildren(childrenTree);

            //查询三级菜单并封装进二级list中
            for (DtNewsSortTreeVo newsSortTreeVo : childrenTree) {
                List<DtNewsSortTreeVo> childrenTree1 = dtNewsSortMapper.findChildrenTree(newsSortTreeVo.getId());
                newsSortTreeVo.setChildren(childrenTree1);

                //查询三级菜单并封装进三级list中
                for (DtNewsSortTreeVo sortTreeVo : childrenTree1) {
                    List<DtNewsSortTreeVo> childrenTree2 = dtNewsSortMapper.findChildrenTree(sortTreeVo.getId());
                    sortTreeVo.setChildren(childrenTree2);
                }
            }
        }
        return parentTree;
    }

    /**
     * 删除类型管理树的结点
     *
     * @param id
     */
    @Override
    public BaseResponse<Boolean> deleteTreeNode(Integer id) {
        //检验id
        if (id == null || id < 1) {
            return BaseResponse.build(SystemCode.QUERY_CHECK_ERROR, "删除异常", Boolean.FALSE);
        }

        //删除
        dtNewsSortMapper.deleteTreeNode(id);
        return BaseResponse.build(SystemCode.SUCCESS, "删除成功", Boolean.TRUE);
    }


    /**
     * 增加类型管理树的结点
     *
     * @param pid
     * @param name
     */
    @Override
    public BaseResponse<Boolean> addTreeNode(Integer pid, String name) {
        //检验
        if (pid == null || pid < 0) {
            return BaseResponse.build(SystemCode.QUERY_CHECK_ERROR, "增加异常", Boolean.FALSE);
        } else if (StringUtils.isBlank(name)) {
            return BaseResponse.build(SystemCode.QUERY_CHECK_ERROR, "名称不可为空", Boolean.FALSE);
        }

        //增加
        dtNewsSortMapper.addTreeNode(pid, name);
        return BaseResponse.build(SystemCode.SUCCESS, "增加成功", Boolean.TRUE);
    }


}
